if(window.plus){
	plusReady();
}else{
	document.addEventListener('plusready',plusReady,false);
}
function plusReady () {
	
	var flightId = getData("flightId");
	var legTh = getData("legTh");
	
	var psgSeats = "";
	var flightDate = "";
	var flightNo = "";
	
	function load_seatInfo () {
		UpdateState();
		loadSeats();
	}
	//加载座位信息
	function loadSeats () {
		var seatsInfo = getData('PsgInfo'+flightId+legTh);
		if (!seatsInfo) {
			plus.nativeUI.alert("本地无数据，请连接网络刷新数据！");
			window.location.href = "index.html";
			return;
		}
		var flight_data = seatsInfo.msg[0];
		var datas = flight_data.seats;
		
		if (seatsInfo.type != "success") {
			plus.nativeUI.alert(seatsInfo.msg);
		} else{
			
			//flightNo
			$("h4#flightNo").append(flight_data.flightNo);
			
			//people
			var peopleInfo = "当前舱单人数："+ (flight_data.ad+flight_data.chd+flight_data.inf) +" (成人："+ flight_data.ad +" / 儿童："+ flight_data.chd +" / 婴儿："+ flight_data.inf +")";
			$("#people").append(peopleInfo);
			
			//flightInfo
			var div_flight = '<div class="row">'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">起飞：'+ flight_data.origtime +'</h5>'
								+'</div>'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">'+ flight_data.orgi +'</h5>'
								+'</div>	'				
							+'</div>'
							+'<div class="row">'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">落地：'+ flight_data.destime +'</h5>'
								+'</div>'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">'+ flight_data.dest +'</h5>'
								+'</div>'
							+'</div>';
			$($.parseHTML(div_flight)).appendTo($("#flightInfo"));
			
			setSeats ();
		}
		
		
		//seats
		function setSeats () {
			$("#seats").empty();
			for (var i = 32 ; i >= 1 ;i--) {
				var div_col_1 = '<div class="row" style="text-align:center;">'
									+'<div class="row col-xs-5 col-sm-5 col-md-5">'
										+'<div class="col-xs-1 col-sm-1 col-md-1 number-line _'+i+'">'
											+'<div class="number">	'
												+'<span>'+ i +'</span>'
											+'</div>'
										+'</div>'
										+'<div class="row col-xs-11 col-sm-11 col-md-11">';
				var seat_left='';
				for (var j = 5; j >= 3; j--) {
					seat_left += getPsgInfoByXY(j,i);
				}
				var div_col_2 = '</div>'
							+'</div>'
							+'<div class="row col-xs-2 col-sm-2 col-md-2">'
								+'<div class="col-xs-3 col-sm-3 col-md-3"></div>'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<div class="aisle"></div>'
								+'</div>'		
								+'<div class="col-xs-3 col-sm-3 col-md-3"></div>'					
							+'</div>'
							+'<div class="row col-xs-5 col-sm-5 col-md-5">'
								+'<div class="row col-xs-11 col-sm-11 col-md-11">';
				var seat_right='';
				for (var j = 2; j >= 0; j--) {
					seat_right += getPsgInfoByXY(j,i);
				}
				var div_col_3 = '</div>'
								+'<div class="col-xs-1 col-sm-1 col-md-1 number-line _'+i+'">'
									+'<div class="number">	'
										+'<span>'+ i +'</span>'
									+'</div>'
								+'</div>'
							+'</div>'
						+'</div>';
				
				var div_col = div_col_1 + seat_left + div_col_2 + seat_right + div_col_3;
				$($.parseHTML(div_col)).appendTo($("#seats"));
			}
			
			//应急出口
			$("._15").removeClass("number-line").addClass("door").html('<div class="safe-door"><span>应</span><br/><span>急</span><br/><span>出</span><br/><span>口</span></div>');
			$("._16").removeClass("number-line").addClass("door").html('<div class="safe-door"><span>应</span><br/><span>急</span><br/><span>出</span><br/><span>口</span></div>');
			
			//安全门
			$("._1").removeClass("number-line").addClass("door").html('<div class="first-door "><span>舱</span><br/><span>门</span></div>');
			$("._32").removeClass("number-line").addClass("door").html('<div class="first-door "><span>舱</span><br/><span>门</span></div>');
		
		}
		
		function getPsgInfoByXY(x, y) {
			var div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'							
								+'<button class="seatn" data-toggle="modal" data-target="#">'
									+'<span>'
									+'</span>'
								+'</button>'
							+'</div>';
			if (y==1 && (x==5||x==4||x==3)) {
				div_seat ="";
			}
			$.each(datas, function(i,seat) {
				if (seat.x == x && seat.y == y) {
					var psginfo = seat.personinfo;	
					div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'
								+'<button class="seat" id="'+ (y==1&&(x==0||x==1||x==2)?((y-1)*6+x+1):((y-1)*6+x-2)) +'" data-toggle="modal" data-target="#'+ seat.seatName +'-info">'
									+'<span class="name">'+ (psginfo.psgName.length>6?(psginfo.psgName.substring(0,6)+"..."):psginfo.psgName) +'</span>'
								+'</button>'
								+'<div class="modal fade" id="'+ seat.seatName +'-info" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'
										+'<div class="modal-dialog">'
											+'<div class="modal-content">'
												+'<div class="modal-header">'
													+'<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>'
													+'<h5 class="modal-title" id="">乘客信息</h5>'
												+'</div>'
												+'<div class="modal-body" style="padding-bottom: 0px">'
													+'<table class="table" style="text-align: left">'
														+'<thead>'
															+'<tr>'
																+'<th>#</th>'
																+'<th>名称</th>'
																+'<th>内容</th>'
															+'</tr>'
														+'</thead>'
														+'<tbody>'
															+'<tr>'
																+'<td>1</td>'
																+'<td>座位号</td>'
																+'<td>'+ seat.seatName +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>2</td>'
																+'<td>旅客姓名</td>'
																+'<td>'+ psginfo.psgName +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>3</td>'
																+'<td>性别</td>'
																+'<td>'+ (psginfo.psgType == "ADT"?(psginfo.psgGender=="F"?(psginfo.inf==true?"女/带婴儿":"女"):(psginfo.psgGender=="M"?(psginfo.inf==true?"男/带婴儿":"男"):(psginfo.psgGender=="CHD"?"儿童":(psginfo.psgGender == "INF" ? "婴儿":"其他")))):(psginfo.psgType == "CHD"?"儿童":(psginfo.psgType == "INF"?"婴儿":"其他"))) +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>4</td>'
																+'<td>带婴儿</td>'
																+'<td>'+ (psginfo.inf==true?"是":"否") +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>5</td>'
																+'<td>订餐类型</td>'
																+'<td>'+ (psginfo.meal==""?"无":psginfo.meal) +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>6</td>'
																+'<td>目的地</td>'
																+'<td>'+ psginfo.dest +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>7</td>'
																+'<td>生日</td>'
																+'<td>'+ (psginfo.birth==true?"是":"否") +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>8</td>'
																+'<td>会员</td>'
																+'<td>'+ (psginfo.member==true?"是":"否") +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>9</td>'
																+'<td>手机号</td>'
																+'<td>'+ (psginfo.tel==""?"无":psginfo.tel) +'</td>'
															+'</tr>'
														+'</tbody>'
													+'</table>'
												+'</div>'
												+'<div class="modal-footer" style="padding-top: 0px">'
													+'<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>'
												+'</div>'
											+'</div>'
										+'</div>'
									+'</div>'
							+'</div>';
							
				}
			});
			return div_seat;
		}
	}
	//加载抽奖历史
	function load_lottery () {
		var lottery_persons = getData("lottery-person"+flightId+legTh);
		if (lottery_persons == false) {
			return;
		} else{
			var persons = lottery_persons.split(",");
			for (var i=0; i<persons.length; i++) {
				$("#lottery-person").append("<br/>"+persons[i]);
				$("[data-target='#"+persons[i]+"-info']").removeClass("seat").addClass("lottery");
			}
		}
	}
	//校验抽奖结果
	function LotteryPsgSn () {
		var seatsInfo = getData('PsgInfo'+flightId+legTh);
		if (!seatsInfo) {
			plus.nativeUI.alert("本地无数据，请连接网络刷新数据！");
			window.location.href = "index.html";
			return;
		}
		if (seatsInfo.type != "success") {
			plus.nativeUI.alert(seatsInfo.msg);
		} else{
			var flight_data = seatsInfo.msg[0];
			psgSeats = flight_data.seats;
			flightDate = flight_data.flightDate;
			flightNo = flight_data.flightNo;
		}
	}
	function saveLotteryPsgSn (seatNo) {
		LotteryPsgSn ();
		$.each(psgSeats, function(key, value) {
			if (value.seatName == seatNo) {
				setData("LotteryPsgSn"+flightDate+flightNo,(getData("LotteryPsgSn"+flightDate+flightNo)==false?"":getData("LotteryPsgSn"+flightDate+flightNo)+",")+value.o_psg_sn)
			}
		});
	}
	function isInLotteryPsgSn (seatNo) {
		LotteryPsgSn ();
		var isIn = false;
		var LotteryPsgSns = getData("LotteryPsgSn"+flightDate+flightNo);
		var PsgSn = "";
		$.each(psgSeats, function(key, value) {
			if (value.seatName == seatNo) {
				PsgSn = value.o_psg_sn;
			}
		});
		if (LotteryPsgSns == false) {
			return;
		} else{
			var persons = LotteryPsgSns.split(",");
			for (var i=0; i<persons.length; i++) {
				if (PsgSn == persons[i]) {
					isIn = true;
				}
			}
		}
		return isIn;
	}
	
	//抽奖
	function lottery () {
		var lottery = 0;
		var seatNo="";
		var started = false;
		$("#start").on("click",function () {
			startCount();
			started = true;
			$(".seat").css("background","url(images/lottery.gif)");
		});
		$("#stop").on("click",function () {
			if (!started) {
				plus.nativeUI.alert()("请先选择开始！");
				return;
			} else{
				stopCount();
				lottery = getRandom();
				seatNo = $("#"+lottery).attr("data-target").replace("-info","").substring(1);
				while (isInLotteryPsgSn(seatNo)) {
					lottery = getRandom();
					seatNo = $("#"+lottery).attr("data-target").replace("-info","").substring(1);
				}
				saveLotteryPsgSn(seatNo);
				setData("lottery-person"+flightId+legTh,(getData("lottery-person"+flightId+legTh)==false?"":getData("lottery-person"+flightId+legTh)+",")+seatNo)
				$("#"+lottery).removeClass("seat").addClass("lottery");
				$("#"+lottery).removeAttr("style");
				$(".seat").css("background","url(images/person.png)");
				$("#lottery-person").append("<br/>"+seatNo);
				window.location.href = "#"+lottery;
				started = false;
			}
		});
		//获得随机数
		function getRandom () {
			var temp=parseInt(Math.random()*(189+1));
			while ($("#"+temp).length == 0) {
				temp=parseInt(Math.random()*(189+1));
			}
			while (isExists(temp)) {
				temp=parseInt(Math.random()*(189+1));
			}
			return temp;
		}
		//判断是否已经中奖
		function isExists(temp) {
			var isExists = false;
			$(".lottery").each(function () {
				if ($(this).attr('id') == temp) {
					isExists = true;
				}
			});
			return isExists;
		}
		
		//显示时钟效果
		function startCount() {
			var today=new Date();
			var h=today.getHours();
			var m=today.getMinutes();
			var s=today.getSeconds();
			m=checkTime(m);
			s=checkTime(s);
			$("#start-time").html(h+":"+m+":"+s);
			t = setTimeout(startCount,1000);
			//加0
			function checkTime(i) {
				if (i<10) {
					i="0" + i;
				}
				return i;
			}
		}
		function stopCount() {
			clearTimeout(t);
		}
	}
	
	$(document).ready(function() {
		
		UpdateState();
		load_seatInfo ();
		load_lottery ();
		lottery ();
		$("#leave").on("click",function () {
			$("#flightpage").html();
		});
	});
}